package com.yidu.verycd_background.controller;

import com.yidu.util.Tools;
import com.yidu.verycd_background.entity.VerycdUser;
import com.yidu.verycd_background.service.VerycdUserService;
import org.springframework.web.bind.annotation.*;
import util.BaseMessage;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (VerycdUser)表控制层
 *
 * @author makejava
 * @since 2020-07-27 09:51:19
 */
@RestController
@RequestMapping("verycdUser")
public class VerycdUserController {
    /**
     * 服务对象
     */
    @Resource
    private VerycdUserService verycdUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    @ResponseBody
    public VerycdUser selectOne(Integer userId) {

        return this.verycdUserService.selectOne(userId);
    }

    /**
     * 查询所有
     * @param verycdUser
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(VerycdUser verycdUser){
        System.out.println(verycdUser.getPage()+"==="+verycdUser.getOffset()+"=="+verycdUser.getLimit());
        //查询所有的用户
        List<VerycdUser> list = verycdUserService.queryAllByLimit(verycdUser);
        //查询所有的行数
        int count = verycdUserService.selectCount(verycdUser);
        return Tools.getBaseMap(list,count) ;

    }


    /**
     * 删除用户
     * @param userId
     * @return
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public BaseMessage deleteById(Integer userId){
        boolean flag = verycdUserService.deleteById(userId);
        BaseMessage baseMessage = new BaseMessage();
        if(!flag){
            baseMessage.setState(BaseMessage.ERROR);
            baseMessage.setMsg("删除失败");
        }
        return baseMessage;
    }

    /**
     * 增加
     * @param verycdUser
     * @return
     */
    public BaseMessage insert(VerycdUser verycdUser){
        VerycdUser verycdUserPo = verycdUserService.insert(verycdUser);
        BaseMessage baseMessage = new BaseMessage();
        if(verycdUserPo.getUserId()==null){
            baseMessage.setState(BaseMessage.ERROR);
            baseMessage.setMsg("增加用户失败!");
        }
        return baseMessage ;
    }

    /**
     * 修改
     * @param verycdUser
     * @return
     */
    public BaseMessage update(VerycdUser verycdUser){
        VerycdUser verycdUserPo = verycdUserService.update(verycdUser);

        return BaseMessage.getSuccess() ;
    }

    @RequestMapping("insertOrUpdate")
    @ResponseBody
    public BaseMessage insertOrUpdate(VerycdUser verycdUserVo){
        if(verycdUserVo.getUserId()==null){
            return insert(verycdUserVo);
        }else{
            return update(verycdUserVo);
        }
    }

    /**
     * 注册
     * @param verycdUser
     * @return
     */
    @RequestMapping("dyRegist")
    @ResponseBody
    public BaseMessage dyRegist(VerycdUser verycdUser){
        VerycdUser verycdUserPo = verycdUserService.dyRegist(verycdUser);
        BaseMessage baseMessage = new BaseMessage();
        if(verycdUserPo.getUserId()==null){
            baseMessage.setState(BaseMessage.ERROR);
            baseMessage.setMsg("注册用户失败!");
        }
        return baseMessage ;
    }

    /**
     * 登录
     *
     * @param verycdUser 主键
     * @return 单条数据
     */
    @RequestMapping("dyLogin")
    @ResponseBody
    public BaseMessage dyLogin(VerycdUser verycdUser) {

        BaseMessage baseMessage = new BaseMessage();
        VerycdUser verycdUser1=verycdUserService.dyLogin(verycdUser);
        if(verycdUser1==null){
            baseMessage.setState(BaseMessage.ERROR);
            baseMessage.setMsg("登录失败!");
        }else{
            baseMessage.setState(BaseMessage.SUCCESS);
            baseMessage.setMsg("登录失败!");
        }
        return baseMessage;
    }



}